package edu.ccut.titan.tag.report.tag;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import edu.ccut.titan.tag.report.design.ChartsInfo;
import edu.ccut.titan.tag.report.design.CrosstabInfo;
import edu.ccut.titan.tag.report.design.FooterInfo;
import edu.ccut.titan.tag.report.design.HeaderInfo;
import edu.ccut.titan.tag.report.design.ReportDesignInfo;
import edu.ccut.titan.tag.report.design.ReportFieldInfo;
import edu.ccut.titan.tag.report.servlet.IReport;
import edu.ccut.titan.tag.report.servlet.ReportTagInfo;

public class TitanReportTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	private List<Map<String, Object>> source;
	private String template;
	private ReportDesignInfo designInfo;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private Map<String, String> parameter;
	private String title;
	private String type;
	private String sort;
	private String sortField;

	public String getType() {
		if (type == null) {
			return "html";
		}
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public int doStartTag() throws JspException {
		designInfo = new ReportDesignInfo();
		parameter = new HashMap<String, String>();
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		if (source != null) {
			ReportTagInfo report = new ReportTagInfo();
			report.setSource(source);
			report.setTitle(title);
			designInfo.setTitle(title);
			request = (HttpServletRequest) pageContext.getRequest();
			response = (HttpServletResponse) pageContext.getResponse();
			if (template == null) {
				report.setDesignInfo(designInfo);
			} else {
				report.setDesignInfo(null);
				request.setAttribute(IReport.TEMPLETE_PATH, this.template);
			}
			if (sort == null)
				designInfo.setSort("");
			else
				designInfo.setSort(sort);
			if (sortField == null)
				designInfo.setSortField("");
			else
				designInfo.setSortField(sortField);
			report.sendRequest(request, response, getType(), getParameter());
		} else {
			JspWriter writer = pageContext.getOut();
			try {
				writer.print("没有数据");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return SKIP_BODY;
	}

	public List<Map<String, Object>> getSource() {
		return source;
	}

	public void setSource(List<Map<String, Object>> source) {
		this.source = source;
	}

	public String getTemplate() {
		return template;
	}

	public void setTemplate(String template) {
		this.template = template;
	}

	public void addDesignInfo(ReportFieldInfo field) {
		designInfo.addReportField(field);
	}

	public void addChartInfo(ChartsInfo chart) {
		designInfo.addChart(chart);
	}

	public void addCrosstabInfo(CrosstabInfo crosstabInfo) {
		designInfo.addCrosstab(crosstabInfo);
	}

	public void addParameterInfo(String key, String value) {
		parameter.put(key, value);
	}

	public void addHeaderInfo(HeaderInfo headerInfo) {
		designInfo.addHeaderInfo(headerInfo);
	}

	public void addFooterInfo(FooterInfo footerInfo) {
		designInfo.addFooterInfo(footerInfo);
	}

	public ReportDesignInfo getDesignInfo() {
		return designInfo;
	}

	public Map<String, String> getParameter() {
		if (parameter.keySet().size() > 0) {
			return parameter;
		} else {
			return null;
		}
	}

	public void setParameter(Map<String, String> parameter) {
		this.parameter = parameter;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getSortField() {
		return sortField;
	}

	public void setSortField(String sortField) {
		this.sortField = sortField;
	}
}
